package com.education.teach.mvp.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.dn.res.wideget.DnToolbar;
import com.education.teach.R;
import com.education.teach.di.component.DaggerSchoolComponent;
import com.education.teach.di.module.SchoolModule;
import com.education.teach.mvp.contract.SchoolContract;
import com.education.teach.mvp.model.bean.SchoolBean;
import com.education.teach.mvp.presenter.SchoolPresenter;
import com.education.teach.mvp.ui.activity.CounselActivity;
import com.education.teach.mvp.ui.activity.CoursewareActivity;
import com.education.teach.mvp.ui.activity.ExamActivity;
import com.education.teach.mvp.ui.activity.MyCoursewareActivityActivity;
import com.education.teach.mvp.ui.adapter.SchoolAdapter;
import com.jess.arms.base.BaseFragment;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.Unbinder;

import static com.jess.arms.utils.Preconditions.checkNotNull;


public class SchoolFragment extends BaseFragment<SchoolPresenter> implements SchoolContract.View {


    @BindView(R.id.toolbar)
    DnToolbar mToolbar;
    @BindView(R.id.tv_office)
    TextView mTvOffice;
    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;
    Unbinder unbinder;
    // 图片封装为一个数组
    private int[] icons = {R.mipmap.ic_book2, R.mipmap.ic_ask, R.mipmap.ic_books, R.mipmap.ic_yin};
    private String[] iconName = {"考试与作业", "咨询答疑", "课件", "我的课件"};
    private List<SchoolBean> mList = new ArrayList<>();
    private SchoolAdapter mAdapter;

    public static SchoolFragment newInstance() {
        SchoolFragment fragment = new SchoolFragment();
        return fragment;
    }

    @Override
    public void setupFragmentComponent(@NonNull AppComponent appComponent) {
        DaggerSchoolComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .schoolModule(new SchoolModule(this))
                .build()
                .inject(this);
    }

    @Override
    public View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_school, container, false);
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        mToolbar.setMainTitle("自主学习");
        for (int i = 0; i < iconName.length; i++) {
            SchoolBean schoolBean = new SchoolBean();
            schoolBean.setId(i);
            schoolBean.setTitle(iconName[i]);
            schoolBean.setRes(icons[i]);
            mList.add(schoolBean);
        }

        mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        mAdapter = new SchoolAdapter(R.layout.item_school, mList);
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            switch (position) {
                case 0:
                    launchActivity(new Intent(getActivity(), ExamActivity.class));// 考试作业
                    break;
                case 1:
                    launchActivity(new Intent(getActivity(), CounselActivity.class));// 咨询G

                    break;
                case 2:
                    launchActivity(new Intent(getActivity(), CoursewareActivity.class));// 课件

                    break;
                case 3:
                    launchActivity(new Intent(getActivity(), MyCoursewareActivityActivity.class));// 我的课件

                    break;
            }
        });
    }

    @Override
    public void setData(@Nullable Object data) {

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {

    }

}
